package com.certification.controller;

import com.certification.common.utils.Constant;
import com.certification.entity.CertAttachment;
import com.certification.entity.SysUserEntity;
import com.certification.service.CertAttachmentService;
import com.certification.service.SysUserRoleService;
import com.certification.vo.ResponseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author copa
 * @createDate 2022-03-31 16:12
 * @function
 */
@RestController
@Api(tags = {"文件处理"})
public class CertAttachmentController extends AbstractController {

    @Resource
    private CertAttachmentService certAttachmentService;

    @Resource
    private SysUserRoleService sysUserRoleService;

    //@PreAuthorize("hasAnyAuthority('ROLE_PRINCIPAL')")
//    @PostMapping("file/upload")
//    @ApiOperation(value = "文件上传（返回文件id）")
//    public ResponseMessage<Integer> upload(MultipartFile multiFile, HttpSession session) {
//        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
//            if (roleId == Constant.ROLE_PRINCIPAL) {
//                return certAttachmentService.upload(multiFile, session);
//            }
//        }
//        return ResponseMessage.errorMsg("该角色不能调用该接口~");
//    }

    @PostMapping("file/upload")
    @ApiOperation(value = "文件路径加入到数据库（返回文件id）")
    public ResponseMessage<Integer> upload(String url, String originName) {
        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
            if (roleId == Constant.ROLE_PRINCIPAL) {
                return certAttachmentService.uploadFile(url, originName);
            }
        }
        return ResponseMessage.errorMsg("该角色不能调用该接口~");
    }



    //@PreAuthorize("hasAnyAuthority('ROLE_PRINCIPAL')")
//    @GetMapping("file/info/{attachmentId}")
//    @ApiOperation(value = "查看文件信息")
//    public ResponseMessage<CertAttachment> fileInfo(@ApiParam("认证阶段：1-认证申请 2-专业自评 3-进校考察 5-整改提高") @RequestParam Integer certStage,
//                                                    @ApiParam("文件id") @PathVariable("attachmentId") Integer attachmentId) {
//        //Integer userId = JwtUtils.getUserId(request.getHeader("Authorization"));
//        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
//            if (roleId == Constant.ROLE_PRINCIPAL) {
//                return certAttachmentService.getFileInfo(certStage, attachmentId, Math.toIntExact(getUserId()));
//            }
//        }
//        return ResponseMessage.errorMsg("该角色不能调用该接口~");
//    }

    @GetMapping("file/admin/info/{attachmentId}")
    @ApiOperation(value = "查看文件信息(审核员&负责人)")
    public ResponseMessage<CertAttachment> fileInfoAdmin(@ApiParam("文件id") @PathVariable("attachmentId") Integer attachmentId) {
        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
            if (roleId == Constant.ROLE_EXPERT || roleId == Constant.ROLE_PRINCIPAL) {
                return certAttachmentService.getFileInfoAdmin(attachmentId);
            }
        }
        return ResponseMessage.errorMsg("该角色不能调用该接口~");
    }

    //@PreAuthorize("hasAnyAuthority('ROLE_PRINCIPAL')")
//    @GetMapping("file/download/{attachmentId}")
//    @ApiOperation(value = "文件下载")
//    public ResponseEntity<byte[]> fileDownload(@ApiParam("认证阶段：1-认证申请 2-专业自评 3-进校考察 5-整改提高") @RequestParam Integer certStage,
//                                               @ApiParam("文件id") @PathVariable("attachmentId") Integer attachmentId,
//                                               HttpServletRequest request,
//                                               HttpSession session) {
//        if(getUserId() != Constant.ROLE_PRINCIPAL){
//            throw new Exception("")
//        }
//        //Integer userId = JwtUtils.getUserId(request.getHeader("Authorization"));
//        return certAttachmentService.fileDownload(certStage, attachmentId, userId, session);
//    }

    //@PreAuthorize("hasAnyAuthority('ROLE_EXPERT')")
//    @GetMapping("admin/file/download/{attachmentId}")
//    @ApiOperation(value = "文件下载(认证专家)")
//    public ResponseEntity<byte[]> fileDownloadAdmin(@ApiParam("文件id") @PathVariable("attachmentId") Integer attachmentId,
//                                               HttpSession session) {
//        return certAttachmentService.fileDownloadAdmin(attachmentId, session);
//    }

}
